﻿
using Admin.NET.Application.Entity;
using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;

/// <summary>
/// 触发历史服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class DeviceTriggerHistoryService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<DeviceTriggerHistory> _deviceTriggerHistoryRep;
    public DeviceTriggerHistoryService(SqlSugarRepository<DeviceTriggerHistory> deviceTriggerHistoryRep)
    {
        _deviceTriggerHistoryRep = deviceTriggerHistoryRep;
    }

    /// <summary>
    /// 分页查询触发历史
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询触发历史")]
    public async Task<SqlSugarPagedList<DeviceTriggerHistoryOutput>> Page(PageDeviceTriggerHistoryInput input)
    {
        var query = _deviceTriggerHistoryRep.AsQueryable()
            .WhereIF(input.DeviceId>0, u => u.DeviceId == input.DeviceId)
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<DeviceInfo>((u, deviceid) => u.DeviceId == deviceid.Id )
            .LeftJoin<DeiviceTemplateTrigger>((u, deviceid, triggerid) => u.TriggerId == triggerid.Id )
            .LeftJoin<DeviceSlaveTemplate>((u, deviceid, triggerid, slaveid) => u.SlaveId == slaveid.Id )
            .LeftJoin<DeviceSlaveTemplatePoints>((u, deviceid, triggerid, slaveid, pointid) => u.PointId == pointid.Id )
            .Select((u, deviceid, triggerid, slaveid, pointid) => new DeviceTriggerHistoryOutput
            {
                Id = u.Id,
                DeviceId = u.DeviceId, 
                DeviceIdDeviceName = deviceid.DeviceName,
                TriggerId = u.TriggerId, 
                TriggerIdTriggerName = triggerid.TriggerName,
                SlaveId = u.SlaveId, 
                SlaveIdSlaveName = slaveid.SlaveName,
                PointId = u.PointId, 
                PointIdPointName = pointid.PointName,
                DataValue = u.DataValue,
                TriggerStatus = u.TriggerStatus,
                TriggerIsProcess = u.TriggerIsProcess,
                Mark = u.Mark,
                CreateTime = u.CreateTime,
                UpdateTime = u.UpdateTime,
                CreateUserId = u.CreateUserId,
                CreateUserName = u.CreateUserName,
                UpdateUserId = u.UpdateUserId,
                UpdateUserName = u.UpdateUserName,
                IsDelete = u.IsDelete,
            });
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加触发历史
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加触发历史")]
    public async Task<long> Add(AddDeviceTriggerHistoryInput input)
    {
        var entity = input.Adapt<DeviceTriggerHistory>();
        await _deviceTriggerHistoryRep.CopyNew().InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除触发历史
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除触发历史")]
    public async Task Delete(DeleteDeviceTriggerHistoryInput input)
    {
        var entity = await _deviceTriggerHistoryRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _deviceTriggerHistoryRep.FakeDeleteAsync(entity);   //假删除
        //await _deviceTriggerHistoryRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新触发历史
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新触发历史")]
    public async Task Update(UpdateDeviceTriggerHistoryInput input)
    {
        var entity = input.Adapt<DeviceTriggerHistory>();
        await _deviceTriggerHistoryRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取触发历史
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取触发历史")]
    public async Task<DeviceTriggerHistory> Detail([FromQuery] QueryByIdDeviceTriggerHistoryInput input)
    {
        return await _deviceTriggerHistoryRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取触发历史列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取触发历史列表")]
    public async Task<List<DeviceTriggerHistoryOutput>> List([FromQuery] PageDeviceTriggerHistoryInput input)
    {
        return await _deviceTriggerHistoryRep.AsQueryable().Select<DeviceTriggerHistoryOutput>().ToListAsync();
    }

    /// <summary>
    /// 获取设备名称列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DeviceInfoDeviceIdDropdown"), HttpGet]
    [DisplayName("获取设备名称列表")]
    public async Task<dynamic> DeviceInfoDeviceIdDropdown()
    {
        return await _deviceTriggerHistoryRep.Context.Queryable<DeviceInfo>()
                .Select(u => new
                {
                    Label = u.DeviceName,
                    Value = u.Id
                }
                ).ToListAsync();
    }




}
